package cn.itheima.web;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: cuisensen
 * data: 2018/7/1.
 * time: 22:13.
 */
@WebFilter("/index.jsp")
public class IndexFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        HttpSession session=req.getSession();
        if(null!=session.getAttribute("user")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            req.getRequestDispatcher(req.getContextPath()+"/login.jsp").forward(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
